package cdu.javaendwork.Controller.Admin.User;

import cdu.javaendwork.model.sqlModel.Concern;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.User.ConcernServiceImpl;
import cdu.javaendwork.service.Interface.User.ConcernService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/add_del_concern")
public class AddandDelConcern extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user= (User)session.getAttribute("user");
        int concernid = Integer.parseInt(req.getParameter("concernid"));
        ConcernService concernService = new ConcernServiceImpl();
        if(concernService.isConcerned(user.getId(),concernid)){
            Concern concern = concernService.findByuserAndConcernId(user.getId(), concernid);
            concernService.delete(concern.getId());
        }else{
            concernService.insert(user.getId(), concernid);
        }
        String flag = req.getParameter("flag");
        if (flag.equals("0")){
            req.getRequestDispatcher("commentList").forward(req,resp);
        }else {
            req.getRequestDispatcher("my_concern").forward(req,resp);
        }
    }
}
